package com.hy;

/**
 * Created With IntelliJ IDEA.
 * Descriptions:下一个排列
 * nums是给出的排列，那么让你输出下一个排列
 * 如123-->132
 * User:Mr.Du
 * Date:2023/12/4
 * Time:09:56
 */
public class NextPermutation {

    public void nextPermutation(int[] nums) {
        int m = nums.length - 1;
        for(int i = m;i >= 0;i--){
            for(int j = m;j > i;j--){
                if(nums[j] > nums[i]){
                    int tmp = nums[i];
                    nums[i] = nums[j];
                    nums[j] = tmp;
                    reverse(nums,i + 1, m);
                    return;
                }
            }
        }
        reverse(nums, 0, m);
    }

    public void reverse(int[] nums, int i, int j){
        while(i < j){
            int tmp = nums[i];
            nums[i] = nums[j];
            nums[j] = tmp;
            i++;
            j--;
        }
    }
}
